package com.admin.service;

import com.admin.dbdao.SoftwareAdminDao;
import com.apkInfo.util.ApkUtil;
import com.client.bean.SaFeAPK;
import com.data.util.CheckDataUtils;
import com.data.util.DatabaseIdUtils;
import com.data.util.FileResumeUtils;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.data.util.FinalStringUtils.*;

/**
 * 软件管理
 *
 * @author:HingLo
 * @create 2017-09-22 15:18
 **/
@Service
public class SoftwareAdminService {
    @Autowired
    private SoftwareAdminDao softwareAdminDao;


    public List<?> findSoftwareAdminService(int type) {
        List<?> list = softwareAdminDao.findSoftwareAdminDao(type);
        if (!list.isEmpty()) {
            return list;
        } else {
            return null;
        }
    }

    /**
     * 下线软甲的申请列表查看
     *
     * @return
     */
    public List<?> downOfflineSoftwareService() {
        return softwareAdminDao.downOfflineSoftwareDao();
    }

    /**
     * 软件下线申请
     *
     * @param id
     * @return
     */
    public boolean applyDownOfflineService(String id, int state) {
        id = id.trim();
        return softwareAdminDao.applyDownOfflineDao(id, state);
    }

    public List<?> lookSpeakingInfoService(String id) {
        id = id.trim();
        if (CheckDataUtils.stringUtilsID(id)) {
            return softwareAdminDao.lookSpeakingInfoDao(id);
        }

        return null;
    }

    public boolean delSpeakingInfoService(String id) {
        id = id.trim();
        return CheckDataUtils.stringUtilsID(id) && softwareAdminDao.delSpeakingInfoDao(id);
    }

    /**
     * 修改软件的状态
     *
     * @param id
     * @param state
     * @return
     */
    public boolean adminSoftwareStateService(String id, int state) {
        id = id.trim();
        if (!CheckDataUtils.stringUtilsID(id)) {
            return false;
        }

        //删除应用
        if (state == 0) {
            return softwareAdminDao.delSoftwareDao(id);
        } else {
            return softwareAdminDao.updateSoftwareStateDao(id, state);
        }
    }

    public List<SaFeAPK> listSafeService() {

        return softwareAdminDao.listSafeDao();
    }

    public boolean submitSafeService(String content, CommonsMultipartFile apk) {
        DiskFileItem fi = (DiskFileItem) apk.getFileItem();
        File files = fi.getStoreLocation();
        Map<String, Object> map = ApkUtil.getApkInfoUtils(files);
        String md5 = (String) map.get(MD5);
        String apkName = APKPATH + md5 + ".apk";
        //保存到本地
        if (!FileResumeUtils.uploadFile(apkName, apk)) {
            return false;
        }

        String version = (String) map.get(VERSIONNAME);
        String id = DatabaseIdUtils.getDataIdOne();
        SaFeAPK saFeAPK = new SaFeAPK();
        saFeAPK.setId(id);
        saFeAPK.setContent(content);
        saFeAPK.setDate(new Date());
        saFeAPK.setVersion(version);
        saFeAPK.setMd5(md5);
        return softwareAdminDao.saveMassageDao(saFeAPK);
    }
}
